<?php

namespace Draw\Utils;
use Plume\Provider\HttpProvider;
class PubUtils
{
    static function getWxRegUserInfo($url,$meet_id){
        $obj = new \stdClass();
        $obj->meet_id = $meet_id;
        $obj_json = json_encode($obj);
        $obj_base64 = base64_encode($obj_json);
        $wx_reg_json = HttpProvider::http_post($url,array('data'=>$obj_base64));
        return json_decode(base64_decode($wx_reg_json['content']));
    }
    static function explodeUser($url,$meet_id,$sequence,$redis,$insert=false){

           $sequence_all_man_key="all_mans_".$sequence;
           $sequence_all_lady_key="all_ladys_".$sequence;
           $sequence_all_all_key="all_all_".$sequence;
           $sequence_man_key="mans_".$sequence;
           $sequence_lady_key="ladys_".$sequence;
           $sequence_all_key="all_".$sequence;
            $sequence_game_status_key="game_status".$sequence;

           $wx_reg_info = PubUtils::getWxRegUserInfo($url, $meet_id);//调用接口得到签到的人员
            $wx_reg_info_result = $wx_reg_info->result;//result为0时表示成功
            if ($wx_reg_info_result == 0) {
                $all_people = $wx_reg_info->data;//返回的签到人员数组
                shuffle($all_people);
                foreach ($all_people as $key=>$people) {
                            $redis->sAdd($sequence_all_all_key,json_encode($people));
                            if($insert)
                               $redis->sAdd($sequence_all_key,json_encode($people));
                            if($people->sex==1){
                                $redis->sAdd($sequence_all_man_key,json_encode($people));
                                if($insert)
                                $redis->sAdd($sequence_man_key,json_encode($people));
                            }else if($people->sex==2){
                                $redis->sAdd($sequence_all_lady_key,json_encode($people));
                                if($insert)
                                $redis->sAdd($sequence_lady_key,json_encode($people));
                            }
                        }
            //初始化游戏状态
            $redis->set($sequence_game_status_key,0);
            $redis->expire($sequence_game_status_key,60*60*24);
            $redis->expire($sequence_all_man_key,60*60*24);
            $redis->expire($sequence_all_lady_key,60*60*24);
            $redis->expire($sequence_all_all_key,60*60*24);
            $redis->expire($sequence_man_key,60*60*24);
            $redis->expire($sequence_lady_key,60*60*24);
            $redis->expire($sequence_all_key,60*60*24);
                //以下代码为增量代码
                /*$num = count($all_people)-$redis->hLen($sequence_all_all_key);
                if($num>0){
                    $all_people=array_slice($all_people,count($all_people)-$num);
                      foreach ($all_people as $key=>$people) {
                            $redis->hSet($sequence_all_all_key,$key,json_encode($people));
                            $redis->lPush($sequence_all_key,json_encode($people));
                            if($people->sex==1){
                                $redis->hSet($sequence_all_man_key,$key,json_encode($people));
                                $redis->lPush($sequence_man_key,json_encode($people));
                            }else if($people->sex==2){
                                $redis->hSet($sequence_all_lady_key,$key,json_encode($people));
                                $redis->lPush($sequence_lady_key,json_encode($people));
                            }
                        }
                }*/
            }
    }
    static function numToWord($num)
    {
        $chiNum = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
        $chiUni = array('','十', '百', '千', '万', '亿', '十', '百', '千');

        $chiStr = '';

        $num_str = (string)$num;

        $count = strlen($num_str);
        $last_flag = true; //上一个 是否为0
        $zero_flag = true; //是否第一个
        $temp_num = null; //临时数字

        $chiStr = '';//拼接结果
        if ($count == 2) {//两位数
            $temp_num = $num_str[0];
            $chiStr = $temp_num == 1 ? $chiUni[1] : $chiNum[$temp_num].$chiUni[1];
            $temp_num = $num_str[1];
            $chiStr .= $temp_num == 0 ? '' : $chiNum[$temp_num];
        }else if($count > 2){
            $index = 0;
            for ($i=$count-1; $i >= 0 ; $i--) {
                $temp_num = $num_str[$i];
                if ($temp_num == 0) {
                    if (!$zero_flag && !$last_flag ) {
                        $chiStr = $chiNum[$temp_num]. $chiStr;
                        $last_flag = true;
                    }
                }else{
                    $chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;

                    $zero_flag = false;
                    $last_flag = false;
                }
                $index ++;
            }
        }else{
            $chiStr = $chiNum[$num_str[0]];
        }
        return $chiStr;
    }
    static function NumToStr($num){
    if (stripos($num,'e')===false) return $num;
    $num = trim(preg_replace('/[=\'"]/','',$num,1),'"');//出现科学计数法，还原成字符串
    $result = "";
    while ($num > 0){
        $v = $num - floor($num / 10)*10;
        $num = floor($num / 10);
        $result   =   $v . $result;
    }
    return $result;
}
    static function formatName($name)
    {
        $json_name = json_decode($name)?json_decode($name):$name;
        $name="";
        if(is_numeric($json_name)) {
            $str = PubUtils::NumToStr($json_name)."";
            $name = mb_strlen($str,'UTF8') > 15 ? (mb_substr($str, 0, 15,'UTF8') . "...") :$str;
        }else
        {
            $str = $json_name;
            $name = mb_strlen($str,'UTF8') > 15 ? (mb_substr($str, 0, 15,'UTF8') . "...") :$str;
        }
        return $name;
    }
}